package com.ad_hccm.controller.TreatmentHistory;

import com.ad_hccm.controller.Code;
import com.ad_hccm.controller.Result;
import com.ad_hccm.domain.TreatmentHistory.OtherDiseaseTwo;
import com.ad_hccm.exception.BusinessException;
import com.ad_hccm.service.TreatmentHistory.OtherDiseaseTwoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;
import java.util.List;

/**
 * TODO 其他疾病治疗史Controller
 */
@RestController
@RequestMapping("/other_disease_two")
public class OtherDiseaseTwoController {
    @Autowired
    private OtherDiseaseTwoService otherDiseaseTwoService;

    @PostMapping
    public Result save(@RequestBody OtherDiseaseTwo otherDiseaseTwo){
        boolean flag = otherDiseaseTwoService.save(otherDiseaseTwo);
        String msg = flag ? "添加失败" : "添加成功" ;
        return new Result( flag ? Code.SAVE_OK : Code.SAVE_ERR, flag, msg);
    }

    @PutMapping
    public Result update(@RequestBody OtherDiseaseTwo otherDiseaseTwo) {
        boolean flag = otherDiseaseTwoService.update(otherDiseaseTwo);
        String msg = flag ? "修改成功" : "数据修改失败，请重试";
        return new Result(flag ? Code.UPDATE_OK : Code.UPDATE_ERR , flag , msg);
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable String id) {
        if(id.length()!=18) throw new BusinessException(Code.GET_ERR, "请输入18位的身份证");
        List<OtherDiseaseTwo> otherDiseaseTwo = otherDiseaseTwoService.getById(id);
        Integer code = otherDiseaseTwo != null ? Code.GET_OK : Code.GET_ERR;
        String msg = otherDiseaseTwo != null ? "查询成功" : "没有该信息";
        return new Result(code , otherDiseaseTwo , msg);
    }

    @GetMapping
    public Result getAll() {
        List<OtherDiseaseTwo> otherDiseaseTwoList = otherDiseaseTwoService.getAll();
        Integer code = otherDiseaseTwoList != null ? Code.GET_OK : Code.GET_ERR;
        String msg = otherDiseaseTwoList != null ? "查询成功" : "数据查询失败，请重试";
        return new Result(code , otherDiseaseTwoList , msg);
    }

    @DeleteMapping
    public Result delete(@RequestBody OtherDiseaseTwo otherDiseaseTwo){
        boolean flag = otherDiseaseTwoService.delete(otherDiseaseTwo);
        String msg = flag ? "添加失败" : "添加成功" ;
        return new Result( flag ? Code.DELETE_OK : Code.DELETE_ERR, flag, msg);
    }
}
